import requests, unittest
from ddt import ddt, data, file_data, unpack
from A3mall_csh.csh2.common import getdata_csv



@ddt()
class rkao5(unittest.TestCase):

    def setUp(self) -> None:
        print('开始测试')

    def tearDown(self) -> None:
        print('测试结束')

    # 登录
    token = None
    @file_data('./login.yaml')
    @unpack
    def test_rkao5_01(self, username, password, casename, exp):
        r1 = requests.post(url='http://10.59.9.14:7003/api/public/login', data={"username": username, "password": password})
        print(r1.json())
        if r1.json()['data'] != []:
            rkao5.token = r1.json()['data']['token']
            print(rkao5.token)
        # 断言
        self.assertIn(r1.json()['info'], exp)


    # 拼团详情
    spid = None
    def test_rako5_02(self):
        r2 = requests.get(url='http://10.59.9.14:7003/api/group?page=1')
        print(r2.json())
        rkao5.spid = r2.json()['data']['list'][2]['id']
        self.assertEqual(r2.json()['info'], 'ok')

    # 商品详情
    def test_rkao5_03(self):
        r3 = requests.get(url='http://10.59.9.14:7003/api/group/view?id='+str(rkao5.spid))
        print(r3.json())
        self.assertEqual(r3.json()['info'], 'ok')

    # 提交订单
    goods_id = None
    def test_rkao5_04(self):
        r4 = requests.get(url='http://10.59.9.14:7003/api/order/confirm?id='+str(rkao5.spid)+'&type=group&sku_id=&num=1&kid=0&shipping_type=1',
                          headers={'Auth-Token': 'Bearer '+str(rkao5.token)})
        print(r4.json())
        rkao5.goods_id = r4.json()['data']['item'][0]['goods_id']
        self.assertEqual(r4.json()['info'], 'ok')


    # 创建订单
    # @file_data('./create.yaml')
    # @unpack
    @data(*getdata_csv('./create.csv'))
    @unpack
    def test_rkao5_05(self, id, num, casename, exp):
        r5 = requests.post(url='http://10.59.9.14:7003/api/order/create',
                           headers={'Auth-Token': 'Bearer '+str(rkao5.token)},
                           json={"id":id,"type":"group","address_id":45,"store_id":1,"shipping_type":1,"bonus_id":"0","payment":"balance","remarks":"","source":1,"url":
                               "http://10.59.9.14:7003/wap/pages/cart/confirm?id=9&kid=0&sku_id=&num=1&type=group","sku_id":"","num":num,"kid":"0"})
        print(r5.json())
        # 断言
        self.assertIn(r5.json()['info'], exp)




if __name__ == '__main__':
    unittest.main()




